package com.vcar.LDiceClient.amfRequester.sz
{	
    import com.vcar.LDiceClient.config.Config;

    import flash.utils.Dictionary;

    import mx.logging.ILogger;
    import mx.logging.Log;
    import mx.rpc.AbstractOperation;
    import mx.rpc.AsyncToken;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.mxml.RemoteObject;

    /**
     * blade ds的客户端
     * @author rhj
     * 此模块从水煮江山中取出来，
     * 是有job管理的部分
     */    
    public class HxRoService
    {
        private static var log:ILogger=Log.getLogger("Data.HxRoService");		
        public var ro:RemoteObject;		
        public var destination:String;		
        private var _successCall:Dictionary;
        private var _faultCall:Dictionary;		
        private var _completeCall:Dictionary;	
        private var _methodNameDict:Dictionary;		
        public function HxRoService()
        {
            _successCall=new Dictionary(true);
            _faultCall=new Dictionary(true);
            _completeCall=new Dictionary(true);
            _methodNameDict = new Dictionary(true);
            ro=new RemoteObject();	
            ro.showBusyCursor=false;					
            ro.endpoint=Config.EndPoint;
            ro.addEventListener(FaultEvent.FAULT,onFault);		
            ro.addEventListener(ResultEvent.RESULT,onResult);	
        }

        /**
         * 调用远程接口的最终函数
         * @param methodName
         * @param successHandler
         * @param faultHandler
         * @param completeHandler
         * @param args
         * @return
         * 示例：
         * <p>
         * call(Methods.listInBoxMails....);
         * </p>
         * @see Methods
         */        
        public function call(methodName:String,successHandler:Function,faultHandler:Function,completeHandler:Function,...args:Array):AbstractOperation
        {
            ro.destination=Methods.getdestination(methodName);
            log.debug("call: "+ destination+"."+methodName +"("+args.join()+")")
            var op:AbstractOperation = ro.getOperation(methodName);	
            var asyncToken:AsyncToken;
            if(args.length>0){
                asyncToken=op.send.apply(null,args);
            }else{
                asyncToken=op.send();
            }
            _methodNameDict[asyncToken.message.messageId] = methodName;
            if(successHandler!=null) _successCall[asyncToken.message.messageId]=successHandler;
            if(faultHandler!=null) _faultCall[asyncToken.message.messageId]=faultHandler;
            if(completeHandler!=null) _completeCall[asyncToken.message.messageId]=completeHandler;
            return op;
        }

        /**
         * rhj
         * 从理论上来说，一次请求，onResult与onFault只执行一个
         * 所以不论执行哪个，应进行另一个的清理工作。
         * 如果不进行清理工作，_faultCall中很有可能有一个指向函数的指针，
         * 而令页面不能完全销毁。
         * 弱联接，仍建议显式删除
         *
         * */
        private function onResult(evt:ResultEvent):void
        {
            if(_successCall[evt.token.message.messageId]!=null ){
                try {
                    _successCall[evt.token.message.messageId](evt);
                }catch(err:Error){
                    log.debug(err.message);
                }
                delete _successCall[evt.token.message.messageId];
            }
            if(evt.result.success && _completeCall[evt.token.message.messageId]!=null && evt.result.jobId>0){
                JobManager.add(evt.result.jobId,_completeCall[evt.token.message.messageId],_methodNameDict[evt.token.message.messageId]);
                    //delete _completeCall[evt.token.message.messageId]				
            }
            delete _faultCall[evt.token.message.messageId];   

        }

        private function onFault(e:FaultEvent):void
        {
            if(_faultCall[e.token.message.messageId]!=null ){
                _faultCall[e.token.message.messageId]();
            }
            delete _successCall[e.token.message.messageId];
            delete _completeCall[e.token.message.messageId];    
            delete _faultCall[e.token.message.messageId];
            //HxGameUIManager.showModule("cutLink",e.fault.faultDetail);
            //mx.controls.Alert.show("与服务器断开连接，点击确定刷新","",4,null,function(evt:CloseEvent):void{HxGameUIManager.refreshWindow(5)});
            //mx.controls.Alert.show(,"",4,null,function(evt:CloseEvent):void{HxGameUIManager.refreshWindow(5)});
        }
        public function removeCallbackHandler(handler:Function):void
        {
            for(var i:String in _successCall){
                if(_successCall[i]==handler) {
                    delete _successCall[i];
                    break;
                }
            }
            for(var j:String in _faultCall){
                if(_faultCall[j]==handler) {
                    delete _faultCall[i];
                    break;
                }
            }			
        }

        public function getMethodNameForCalledByMessageID(messageId:String):String
        {
            return _methodNameDict[messageId];
        }
    }
}

